package com.atguigu.mvc.controller;

import com.atguigu.mvc.bean.BaseUser;
import com.atguigu.mvc.bean.ExtendUser;
import com.atguigu.mvc.bean.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.Enumeration;

@Controller("ParamTestController")
public class ParamController {


    private Logger logger = LoggerFactory.getLogger(ParamController.class);

    @RequestMapping("/param")
    public String param() {
        return "test_param";
    }

    // /testServletAPI?user_name=lindujia&password=1&password=2&hobby=a&hobby=b&hobby=c
    @RequestMapping("/testServletAPI")
    //形参位置的request表示当前请求
    public String testServletAPI(HttpServletRequest request, String hobby,
                                 @RequestParam(value = "username", required = false, defaultValue = "") String[] username) {
        HttpSession session = request.getSession();
        String sessionId = session.getId();
        System.out.println("sessionId = " + sessionId);
        String contentType = request.getContentType();
        String characterEncoding = request.getCharacterEncoding();
        System.out.println("contentType = " + contentType);
        System.out.println("characterEncoding = " + characterEncoding);
        String usernameParam = request.getParameter("username");  // 控制台乱码问题
        String password = request.getParameter("password");
        System.out.println("usernameParam:" + usernameParam + ",password:" + password);
        System.out.println("username = " + Arrays.toString(username) + ", length=" + username.length);

        String hobbyParam = request.getParameter("hobby");
        System.out.println("hobby = " + hobby);
        System.out.println("hobbyParam = " + hobbyParam);

        String[] usernames = request.getParameterValues("username");
        System.out.println("usernames = " + Arrays.toString(usernames));
        Enumeration<String> parameterNames = request.getParameterNames();
        while (parameterNames.hasMoreElements()) {
            String key = parameterNames.nextElement();
            String val = request.getParameter(key);
            String[] values = request.getParameterValues(key);

            logger.info("key={}, val={}, values={}, length={}", key, val, Arrays.toString(values), values.length);
        }
        return "success";
    }


    @RequestMapping("/testParam")
    public String testParam(
            @RequestParam(value = "user_name", required = false, defaultValue = "hehe") String username,
            String password,
            String[] hobby,
            @RequestHeader(value = "sayHaha", required = true, defaultValue = "haha") String host,
            @CookieValue("JSESSIONID") String JSESSIONID) {
        //若请求参数中出现多个同名的请求参数，可以再控制器方法的形参位置设置字符串类型或字符串数组接收此请求参数
        //若使用字符串类型的形参，最终结果为请求参数的每一个值之间使用逗号进行拼接
        logger.info("username:" + username + ",password:" + password + ",hobby:" + Arrays.toString(hobby));
        System.out.println("host:" + host);
        System.out.println("JSESSIONID:" + JSESSIONID);
        return "success";
    }


    @RequestMapping("/testBean")
    public String testBean(User user) {
        System.out.println(user);
        return "success";
    }

    @RequestMapping("/testBean2")
    public String testBean2(BaseUser baseUser, ExtendUser extendUser) {
        logger.info("baseUser={}, extendUser={}", baseUser, extendUser);
        return "success";
    }
}
